import http from "Http"
import tips from "Tips"
const common = {
    user: function (that, cb) {
        var user = wx.getStorageSync('user');
        console.log(user)
        if (!user || !user.unid) {
            wx.getSetting({
                success: function (res) {
                    if (res.authSetting['scope.userInfo']) {
                        // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                        //shouquan == 2 表示 弹出请等待.. 页面
                        that.setData({
                            shouquan: 2
                        })
                        // wx.login({
                        //     success: function (res) {
                        //         console.log(res)
                        //         wx.getUserInfo({
                        //             success: function (userres) {
                        //                 console.log(userres)
                        //             }
                        //         })
                        //     }
                        // })

                        wx.login({
                            success: function (res) {
                                console.log(res)
                                if (res.code) {
                                    var udata = {
                                        code: res.code
                                    }
                                    http.req('/Wxapi/getOpenId', udata, function (res) {
                                        console.log(res)
                                        var edata = {
                                            openid: res.openid,
                                        }
                                        if (res) {
                                            wx.getUserInfo({
                                                success: function (userres) {
                                                    console.log(userres)
                                                    edata.wx_name = userres.userInfo.nickName
                                                    edata.wx_img = userres.userInfo.avatarUrl
                                                    var unid_data = {
                                                        session_key: res.session_key,
                                                        encryptedData: userres.encryptedData,
                                                        iv: userres.iv
                                                    }
                                                    console.log(unid_data)
                                                    http.req('/Wxapi/getUserinfo', unid_data, function (unres) {
                                                        console.log(unres)
                                                        edata.unid = unres.unionId
                                                        console.log(edata)
                                                        http.req('/Wxapi/add_user', edata, function (res) {
                                                            edata.userid = res.id
                                                            edata.log = res.log
                                                            wx.setStorageSync('user', edata);
                                                            return typeof cb == "function" && cb(edata)
                                                        })
                                                    })


                                                }
                                            })
                                        }

                                    });
                                }
                            },

                        });
                    } else {
                        that.setData({
                            shouquan: 1
                        })
                    }
                }
            })

        }


    },
    log_user: function (that, cb) {
        var user = wx.getStorageSync('user')
        if (user.log == 1) {
            return typeof cb == "function" && cb(user)
        } else {
            if (!user) {
                that.setData({
                    shouquan: 1
                })
            } else {
                that.setData({
                    bangdingtoocletag: true
                })
            }

        }
    },
    log_user_unid: function (that, cb) {
        var user = wx.getStorageSync('user')
        if(!user.unid){
            that.setData({
                shouquan: 1
            })
        }else{
            if (user.log == 1) {
                return typeof cb == "function" && cb(user)
            } else {
                if (!user) {
                    that.setData({
                        shouquan: 1
                    })
                } else {
                    that.setData({
                        bangdingtoocletag: true
                    })
                }

            }
        }

    },
    getPhoneNumber: function (e,that) {
        wx.login({
            success: function (res) {
                if (res.code) {
                    //发起网络请求
                    console.log(res.code)
                    var udata = {
                        code: res.code
                    }
                    http.req('/Wxapi/getOpenId', udata, function (res) {
                        if (res) {
                            var phone_data = {
                                session_key: res.session_key,
                                encryptedData: e.detail.encryptedData,
                                iv: e.detail.iv
                            }
                            console.log(phone_data)

                            http.req("/Wxapi/getPhone", phone_data, function (pres) {
                                console.log(pres)
                              if (pres){
                                if (pres.co == 200) {
                                  that.setData({
                                    bangdingtoocletag: false
                                  })
                                  tips.loading("登录中。。。");
                                  var login_data = {
                                    login: pres.tel,
                                    passwd: pres.tel,
                                    uid: wx.getStorageSync('user').userid
                                  };
                                  http.req("/Api/log", login_data, function (logres) {
                                    console.log(logres)
                                    tips.loaded();
                                    if (logres.co == 200) {
                                      tips.ok("登录成功!")
                                      var user = wx.getStorageSync('user')
                                      user.log = 1
                                      wx.setStorageSync('user', user)
                                      that.setData({
                                        shengyihao: pres.tel
                                      })
                                      that.setData({
                                        userinfo: user,
                                        shengyihao: pres.tel
                                      })
                                    } else {
                                      tips.error(logres.msg_val)
                                    }
                                  })
                                } else {
                                  tips.error(pres.msg_val)
                                }
                              } else {
                                  tips.error("网络延迟!")
                                }
                                
                            })
                        }

                    })
                } else {
                    console.log('获取用户登录态失败！' + res.errMsg)
                }
            }
        });
    }


}
export default common;